package service

import (
	"github.com/gin-gonic/gin"
	"groupchat/basic/config"
	"groupchat/handler/model"
	"groupchat/handler/request"
	"net/http"
)

func AddFriend(c *gin.Context) {
	var form request.AddFriend
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	var friend model.Friend
	if err := friend.IsFriend(config.DB, form.UserId, form.FriendId); err == nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "好友已存在",
		})
		return
	}
	friend = model.Friend{
		UserId:   form.UserId,
		FriendId: form.FriendId,
		ReMark:   form.ReMark,
	}
	if err := friend.AddFriend(config.DB); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "发送好友申请失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "发送好友申请成功",
	})
	return
}
